/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDialog.java
 *
 * Created on 22.Şub.2010, 14:15:46
 */
package org.testis.gui.mvc;

import org.testis.gui.mvc.utilities.AeSimpleMD5;
import org.testis.gui.mvc.view.AtofBaseDialog;
import org.testis.utils.UserSettingsManager;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.testis.utils.middleware.UserSession;

/**
 *
 * @author nurhat
 */
public class LoginDialog extends AtofBaseDialog {

    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    private String userName = "";
    private String password = "";
    private String fromMemory = "";

    /** Creates new form LoginDialog */
    public LoginDialog(java.awt.Frame parent, boolean modal) {

        super(parent, modal);
        boolean rememberMe = UserSettingsManager.getInstance().isRememberMe();
        if (rememberMe) {
            userName = UserSettingsManager.getInstance().getUserName();
            password = UserSettingsManager.getInstance().getPassword();
            fromMemory = new String(password);
        }
        initComponents();
        invalidUserNameLbl.setVisible(false);
        chkRememberMe.setSelected(rememberMe);


    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }
    // centers the dialog within the screen [1.1]

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        chkRememberMe = new javax.swing.JCheckBox();
        invalidUserNameLbl = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField(password);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("User Name:");

        txtUserName.setText(userName);

        jLabel2.setText("Password:");

        chkRememberMe.setText("Remember Me!");

        invalidUserNameLbl.setForeground(new java.awt.Color(255, 0, 0));
        invalidUserNameLbl.setText("Invalid username/password");

        txtPassword.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtPasswordPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(255, Short.MAX_VALUE)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkRememberMe)
                    .addComponent(invalidUserNameLbl)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                        .addComponent(txtUserName)))
                .addContainerGap(88, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(chkRememberMe)
                .addGap(7, 7, 7)
                .addComponent(invalidUserNameLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        String inpass = new String(txtPassword.getPassword());
        if (!inpass.isEmpty() && !inpass.equals(fromMemory)) {
            try {
                inpass = AeSimpleMD5.MD5(inpass);
            } catch (NoSuchAlgorithmException ex) {
            } catch (UnsupportedEncodingException ex) {
            }
        }
        if (UserSession.newInstance().isValidLogin(txtUserName.getText().trim(), inpass)) {
            if (chkRememberMe.isSelected()) {
                UserSettingsManager.getInstance().setPassword(inpass);
                UserSettingsManager.getInstance().setUserName(txtUserName.getText());
                UserSettingsManager.getInstance().setRememberMe(chkRememberMe.isSelected());
            }
            doClose(RET_OK);
        } else {
            invalidUserNameLbl.setVisible(true);
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void txtPasswordPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtPasswordPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordPropertyChange

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JCheckBox chkRememberMe;
    private javax.swing.JLabel invalidUserNameLbl;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton okButton;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;
}
